namespace Vanilla.Tools.DirectoryPreviews

import Vanilla.UI
import Vanilla

class DirectoryPreviewsTool (UI.Tool, IGenerateThumbnail):
	ToolDirectory as File
	
	FolderBack as BitmapCanvas
	FolderFront as BitmapCanvas

	def constructor (f as File):
		ToolDirectory = f
		FolderBack = BitmapCanvas.Load(File(f.Path + "/folder-back.png"))
		FolderFront = BitmapCanvas.Load(File(f.Path + "/folder-front.png"))
		print "ToolDirectory=\"${f.Path}\" FolderBack=${FolderBack} FolderFront=${FolderFront}"

	def GenerateThumbnail (dir as File, sz as Size):
		thumb = BitmapCanvas(sz.Width, sz.Height)
		using brush = Brush(thumb):
			/*brush.LineWidth = 2
			brush.MoveTo(2, 7)
			brush.LineTo(7, 2)
			brush.LineTo(27, 2)
			brush.LineTo(32, 7)
			brush.LineTo(sz.Width-2, 7)
			brush.LineTo(sz.Width-2, sz.Height-2)
			brush.LineTo(2, sz.Height-2)
			brush.LineTo(2,7)
			
			brush.Colour = Colour(0.8,0.8,0.2,0.5)
			brush.FillPreserve()
			
			brush.Colour = Colour.Grey
			brush.Stroke()*/
			
			// Draw back
			brush.Save()
			brush.Scale(sz.Width / FolderBack.Size.Width, sz.Height / FolderBack.Size.Height)
			using brush.Pattern = CanvasPattern(FolderBack):
				brush.Paint()
			brush.Restore()
			
			// Draw contents
			count = 0
			max = 3
			brush.Save()
			brush.Translate(0,5)
			for i in dir.Contents:
				if i.Type.HasToolForAction("GenerateThumbnail") and i.Type.Name != "Directory" and count < max:
					t = (i.Type.ToolForAction("GenerateThumbnail") as IGenerateThumbnail).GenerateThumbnail(i, Size(sz.Width/2, sz.Height/2))
					brush.Translate(10,5)
					using brush.Pattern = CanvasPattern(t):
						brush.Paint()
					count++
			brush.Restore()
			
			// Draw front
			brush.Scale(sz.Width / FolderFront.Size.Width, sz.Height / FolderFront.Size.Height)
			using brush.Pattern = CanvasPattern(FolderFront):
				brush.Paint()
				
		return thumb
